package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivityTaozhuang;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.custom.MyRecyclerView;
import nero.yncc.com.createart.interfaces.InterfaceDoSomeThing;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.TextUtil;
import nero.yncc.com.createart.utils.ToastUtil;

/**
 * Created by nero on 17-7-6.
 */

public class AdapterTaozhuangin extends RecyclerView.Adapter<AdapterTaozhuangin.Holder> {
    Context context;
    ArrayList<GodBean> arrayList;
    ActivityTaozhuang activityTaozhuang;

    public AdapterTaozhuangin(Context context, ArrayList<GodBean> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
        activityTaozhuang = (ActivityTaozhuang) context;
    }

    @Override
    public AdapterTaozhuangin.Holder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_taozhuang_in, null);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(AdapterTaozhuangin.Holder holder, int i) {
        final GodBean godBean = arrayList.get(i);
        final GodBean.Goods goods = godBean.getGoods();
        ImageUtils.BindImage(context, holder.iv_image, goods.getOriginal_img(), 1);

        if (goods.isChoose()) {
            holder.icon_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
        } else {
            holder.icon_choose.setTextColor(context.getResources().getColor(R.color.textColorGray));
        }

        holder.tv_title.setText(TextUtil.getText(goods.getGoods_name(), "暂无名称"));

        holder.tv_money.setText("￥" + TextUtil.getText(goods.getShop_price(), "0"));
//        }catch (Exception e){
//            //价格计算错误 启动不能点击操作
//            holder.tv_money.setText("该商品价格有误，无法购买");
//            goods.setNum(0);
//            goods.setChoose(false);
//            this.notifyDataSetChanged();
//        }


        holder.tv_num.setText("×" + TextUtil.getText(goods.getNum() + "", "0"));
        holder.tv_num_count.setText(TextUtil.getText(goods.getNum() + "", "0"));
        holder.tv_money2.setText("￥" + TextUtil.getText(goods.getMarket_price(), "0"));
        holder.tv_money2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        final ArrayList<GodBean.SpecAttr> arrayList_spec = goods.getGoods_spec_list();

        holder.icon_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (GodBean.SpecAttr specAttr : arrayList_spec) {
                    if (TextUtils.isEmpty(specAttr.getChooseAttrId())) {
                        ToastUtil.showToast(context, "请选择"+specAttr.getName());
                        return;
                    }
                }

                if(goods.getStore_count().equals(0)){
                    ToastUtil.showToast(context, "该商品没有库存,无法购买");
                    return;
                }

                if (goods.isChoose()) {
                    goods.setChoose(false);
                } else {
                    goods.setChoose(true);
                }
                activityTaozhuang.setGuigeMoney();

                notifyDataSetChanged();
            }
        });

        holder.tv_cut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int numcache = goods.getNum();
                if (numcache > 1) {
                    goods.setNum(--numcache);
                    boolean canChoose=true;
                    for (GodBean.SpecAttr specAttr : arrayList_spec) {
                        if (TextUtils.isEmpty(specAttr.getChooseAttrId())) {
                            canChoose=false;
                        }
                    }
                    goods.setChoose(canChoose);
                    notifyDataSetChanged();
                    activityTaozhuang.setGuigeMoney();
                }

            }
        });

        holder.tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int numcache = goods.getNum();
                try {
                    if (numcache < Integer.parseInt(goods.getStore_count())) {
                        goods.setNum(++numcache);
                        boolean canChoose=true;
                        for (GodBean.SpecAttr specAttr : arrayList_spec) {
                            if (TextUtils.isEmpty(specAttr.getChooseAttrId())) {
                                canChoose=false;
                            }
                        }
                        goods.setChoose(canChoose);
                        notifyDataSetChanged();
                    }
                    activityTaozhuang.setGuigeMoney();
                } catch (NumberFormatException e) {
                    ToastUtil.showToast(context, "库存不够啦");
                    return;
                }

            }
        });


        if (arrayList_spec.size() != 0) {
            AdapterGoodsSpec adapterGoodsSpec = new AdapterGoodsSpec(context, arrayList_spec, new InterfaceDoSomeThing() {
                @Override
                public void doSomething() {
                    String key = "";
                    for (GodBean.SpecAttr specAttr : arrayList_spec) {
                        if (TextUtils.isEmpty(specAttr.getChooseAttrId())) {
                            return;
                        }
                        key += specAttr.getChooseAttrId() + "_";
                    }
                    key = key.substring(0, key.length() - 1);
                    try {
                        JSONObject jsonObject = new JSONObject(godBean.getSpec_goods_price()).getJSONObject(key);
                        goods.setShop_price(jsonObject.getString("price"));
                        goods.setMarket_price(jsonObject.getString("market_price"));
                        AdapterTaozhuangin.this.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    activityTaozhuang.setGuigeMoney();
                }
            });
            holder.lv_list_guige.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
            holder.lv_list_guige.setAdapter(adapterGoodsSpec);
        }

    }

    class Holder extends RecyclerView.ViewHolder {
        final IconView icon_choose;
        final ImageView iv_image;
        final TextView tv_title, tv_num, tv_money, tv_cut, tv_num_count, tv_add, tv_money2;
        final MyRecyclerView lv_list_guige;

        public Holder(View root) {
            super(root);
            icon_choose = root.findViewById(R.id.icon_choose);
            iv_image = root.findViewById(R.id.iv_image);
            tv_title = root.findViewById(R.id.tv_title);
            tv_num = root.findViewById(R.id.tv_num);
            tv_money = root.findViewById(R.id.tv_money);
            tv_cut = root.findViewById(R.id.tv_cut);
            tv_num_count = root.findViewById(R.id.tv_num_count);
            tv_add = root.findViewById(R.id.tv_add);
            lv_list_guige = root.findViewById(R.id.lv_list_guige);
            tv_money2 = root.findViewById(R.id.tv_money2);
        }
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }
}
